package com.zdx.yswl.tool;

import java.lang.reflect.Method;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

public class KSystemTool {

	public static final String TAG = KSystemTool.class.getSimpleName();

	/**
	 * 得到sdk 版本�?	 * @return
	 */
	public static int getSdkVersion() {
		return Build.VERSION.SDK_INT;
	}

	/**
	 * 判断网络是否可用
	 * @param context 上下文	 * @return
	 */
	public static boolean isNetworkAvailable(Context context) {
		try {
			ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo netWorkInfo = cm.getActiveNetworkInfo();
			return (netWorkInfo != null && netWorkInfo.isAvailable());// �?��网络是否可用
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 得到版本号	 
	 * @param context 
	 * @return
	 */
	public static int getAppVersionCode(Context context) {
		try {
			PackageManager packageManager = context.getPackageManager();
			PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
			return packInfo.versionCode;
		} catch (NameNotFoundException e) {
			return -1;
		}
	}

	/**
	 * 得到版本名称
	 * @param __context
	 * @return
	 */
	public static String getAppVersionName(Context context) {
		try {
			PackageManager packageManager = context.getPackageManager();
			PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
			return packInfo.versionName;
		} catch (NameNotFoundException e) {
			return null;
		}
	}

	

public static String getDeviceInfo(Context context) {
    try{
      org.json.JSONObject json = new org.json.JSONObject();
      android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) context
          .getSystemService(Context.TELEPHONY_SERVICE);

      String device_id = tm.getDeviceId();

      android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) context.getSystemService(Context.WIFI_SERVICE);

      String mac = wifi.getConnectionInfo().getMacAddress();
      json.put("mac", mac);

      if( TextUtils.isEmpty(device_id) ){
        device_id = mac;
      }

      if( TextUtils.isEmpty(device_id) ){
        device_id = android.provider.Settings.Secure.getString(context.getContentResolver(),android.provider.Settings.Secure.ANDROID_ID);
      }

      json.put("device_id", device_id);

      KLog.e("-------------友盟集成测试---------- ：", json.toString());
      return json.toString();
    }catch(Exception e){
      e.printStackTrace();
    }
  return null;
}
                  
	
	public static String getPhoneNum(Context _context) {
		TelephonyManager phoneMgr = (TelephonyManager) _context.getSystemService(Context.TELEPHONY_SERVICE);
		return phoneMgr.getLine1Number();
	}

	public static String getIMEI(Context _context) {
		TelephonyManager _manager = (TelephonyManager) _context.getSystemService(Context.TELEPHONY_SERVICE);
		String _imei = _manager.getDeviceId(); // 取出IMEI
		return _imei;
	}

	public static String getICCID(Context _context) {
		TelephonyManager _manager = (TelephonyManager) _context.getSystemService(Context.TELEPHONY_SERVICE);
		String _iccid = _manager.getSimSerialNumber(); // 取出ICCID
		return _iccid;
	}

	public static String getIMSI(Context _context) {
		TelephonyManager _manager = (TelephonyManager) _context.getSystemService(Context.TELEPHONY_SERVICE);
		String _imsi = _manager.getSubscriberId(); // 取出IMSI
		return _imsi;
	}

	public static boolean isPad(Context _context) {
		if (android.os.Build.VERSION.SDK_INT >= 11) {
			Configuration con = _context.getResources().getConfiguration();
			try {
				Method mIsLayoutSizeAtLeast = con.getClass().getMethod("isLayoutSizeAtLeast", int.class);
				return (Boolean) mIsLayoutSizeAtLeast.invoke(con, 0x00000004);
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
		}
		return false;
	}

	public static Intent call_1(String _phoneNum) {
		return new Intent(Intent.ACTION_CALL_BUTTON, Uri.parse("tel:" + _phoneNum));
	}

	public static Intent call_2(String _phoneNum) {
		return new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + _phoneNum));
	}

	/**
	 * 得到设备的唯一Id
	 * @return
	 */
	public static String getInstallationId() {
		return android.os.Build.SERIAL;
	}
	
	
	/**
	 * 得到设备屏幕的宽度
	 */
	public static int getScreenWidth(Context context) {
		return context.getResources().getDisplayMetrics().widthPixels;
	}

	/**
	 * 得到设备屏幕的高度
	 */
	public static int getScreenHeight(Context context) {
		return context.getResources().getDisplayMetrics().heightPixels;
	}

	/**
	 * 得到设备的密度
	 */
	public static float getScreenDensity(Context context) {
		return context.getResources().getDisplayMetrics().density;
	}

	/**
	 * 把密度转换为像素
	 */
	public static int dip2px(Context context, float px) {
		final float scale = getScreenDensity(context);
		return (int) (px * scale + 0.5);
	}
}
